package edu.nf.meituan.dao.user.impl;

import edu.nf.meituan.dao.user.ShoppingDAO;
import edu.nf.meituan.model.order.user.UserShopPing;
import edu.nf.meituan.model.user.Shopping;
import edu.nf.util.DBUtils;
import org.nf.db.util.SqlExecutor;
import org.nf.db.util.result.BeanListHandler;

import java.nio.channels.SelectableChannel;
import java.util.List;

/**
 * @Date 2023/6/19
 * @Author zgm
 */
public class ShopPingDAOImpl implements ShoppingDAO {
    @Override
    public int addShopPing(Shopping shopping) {
        SqlExecutor executor = new SqlExecutor(DBUtils.getConnection());
        String sql = "INSERT INTO shopping(u_id,items_id,shop_id,item_num,shopping_status) VALUES(?,?,?,?,0)";
        return executor.executeUpdate(sql,
                shopping.getUserId(),
                shopping.getItemId(),
                shopping.getShopId(),
                shopping.getItemNum());
    }

    @Override
    public List<UserShopPing> listShopping(int userId, int shopId) {
        SqlExecutor executor = new SqlExecutor(DBUtils.getConnection());
        String sql = "SELECT shopping.shopping_id, shopping.u_id, shopping.item_num,items.items_picture, items.items_id, items.items_name, items.items_price "+
                " FROM shopping INNER JOIN items ON shopping.items_id = items.items_id "+
                " where shopping.u_id = ? and shopping.shop_id = ? and shopping_status = 0";
        return executor.executeQuery(sql,
                new BeanListHandler<>(UserShopPing.class),
                userId,shopId);
    }

    @Override
    public int updateShopPing(int userId, int shopId) {
        SqlExecutor executor = new SqlExecutor(DBUtils.getConnection());
        String sql = "UPDATE shopping SET shopping_status = 1 WHERE u_id = ? AND shop_id = ?;";
        return executor.executeUpdate(sql,userId,shopId);
    }
}
